package database.example.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

import database.example.dao.DepartmentDao;
import database.example.dto.Department;

public class DepartmentService extends AbsService<Department>{
	ArrayList<Department> list;
	
	public DepartmentService(Scanner sc, Connection connection) {
		super(sc, connection);
		dao = new DepartmentDao(connection);
	}

	
	@Override
	public Department inputItemInfo(boolean b) {
		System.out.println("부서번호 부서명");
		Department dept = new Department(sc.nextInt(), sc.next());
		if(b){
			System.out.println("위치");
			dept.setFloor(sc.nextInt());			
		}
		return dept;
	}
	
	@Override
	public void prnSubMenu() {
		System.out.println("1. 부서명 정렬 2. 위치 정렬 3. 부서번호순 정렬");
		try {
			list = dao.listItems();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		int res = sc.nextInt();
		switch (res) {
			case 1://	
				prnItemList(new Comparator<Department>() {

					@Override
					public int compare(Department o1, Department o2) {
						return o1.getDeptName().hashCode() - o2.getDeptName().hashCode();
					}
				});
				break;
			case 2://
				prnItemList(new Comparator<Department>() {
					
					@Override
					public int compare(Department o1, Department o2) {
						return o1.getFloor() - o2.getFloor();
					}
				});
				break;
			default://
				prnItemList(null);
				
		}	
		
	}


	private void prnItemList(Comparator<Department> comparator) {
		if(comparator == null){
			Collections.sort(list);			
			prnList(list);
		}
		else{
			Collections.sort(list, comparator);
			prnList(list);
		}

	}


	private void prnList(ArrayList<Department> list) {
		for(Department dept : list){
			System.out.println(dept);
		}
		
	}
	
	
}
